#include"udpServer.hpp"
#include<memory>
#include<fstream>
#include<unordered_map>

using namespace std;
using namespace Server;

static void Usage(string proc)
{
    cout << "Usage:\n\t" << proc << " local_port\n\n";
}

//demo1
void handlerMessage(int sockfd, string clientip, uint16_t clientport, string message)
{
    //就可以对message进行特定的业务处理，而不关心message是怎么来的 --- server通信和业务逻辑解耦！
    string response_message = message;
    response_message += " [server echo]";

    //开始返回
    struct sockaddr_in client;
    bzero(&client, sizeof(client));

    client.sin_family = AF_INET;
    client.sin_port = htons(clientport);
    client.sin_addr.s_addr = inet_addr(clientip.c_str());

    sendto(sockfd, response_message.c_str(), response_message.size(), 0, (struct sockaddr*)&client, sizeof(client));


}

// 使用方式：./udpServer  port
int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(UsAGE_ERR);
    }

    uint16_t port = atoi(argv[1]);
    
    std::unique_ptr<udpServer> usvr(new udpServer(handlerMessage, port));
    usvr->initServer();
    usvr->start();

    return 0;
}